﻿using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System.Collections.Generic;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Net
{
    public partial class AsycUdpClient
    {
        /// <summary>
        /// 发送数据线程
        /// </summary>
        void SendThread()
        {
            while (Connected)
            {
                //发送缓冲过去中的数据给服务器
                for (int i = 0; i < outMessages.Count; i++)
                {
                    while (outMessages[i] == null) ;

                    if (DebugInfo.Data) Debug("Sent " + outMessages[i].Size);

                    udpSocket.Send(outMessages[i].Array, outMessages[i].Size);
                    upByteBuffer += outMessages[i].Size;
                    upByteTotal += outMessages[i].Size;
                }

                outMessages.Clear();

                Thread.Sleep(1);
            }
        }
        
        public void Send(MessageBuffer msg)
        {
            outMessages.Add(msg);
        }
    }
}

